﻿namespace OleDbDAL.Procurement
{
    using DBUtility;
    using IDal.Procurement;
    using Model.Procurement;
    using System;
    using System.Data;
    using System.Text;

    public class ProcurementPaymentListApp : IProcurementPaymentList
    {
        private string dbname = "Systems";

        public void Add(ProcurementPaymentListEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_ProcurementPaymentList(");
            builder.Append("PurchaseId,ProcurementId,PayMentMoney");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("'" + model.PurchaseId + "',");
            builder.Append("'" + model.ProcurementId + "',");
            builder.Append("" + model.PayMentMoney + "");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int PaymentListId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_ProcurementPaymentList ");
            builder.Append(" where PaymentListId=" + PaymentListId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_ProcurementPaymentList ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by PaymentListId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "PaymentListId", "T_ProcurementPaymentList", swhere);
        }

        public ProcurementPaymentListEnt GetModel(int PaymentListId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select PaymentListId,PurchaseId,ProcurementId,PayMentMoney from T_ProcurementPaymentList ");
            builder.Append(" where PaymentListId=" + PaymentListId);
            ProcurementPaymentListEnt ent = new ProcurementPaymentListEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.PaymentListId = PaymentListId;
            if (table.Rows.Count > 0)
            {
                ent.PurchaseId = int.Parse(table.Rows[0]["PurchaseId"].ToString());
                ent.ProcurementId = int.Parse(table.Rows[0]["ProcurementId"].ToString());
                if (table.Rows[0]["PayMentMoney"].ToString() != "")
                {
                    ent.PayMentMoney = decimal.Parse(table.Rows[0]["PayMentMoney"].ToString());
                }
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_ProcurementPaymentList ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "PaymentListId");
        }

        public void Update(ProcurementPaymentListEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_ProcurementPaymentList set ");
            builder.Append("PurchaseId='" + model.PurchaseId + "',");
            builder.Append("ProcurementId='" + model.ProcurementId + "',");
            builder.Append("PayMentMoney=" + model.PayMentMoney + "");
            builder.Append(" where PaymentListId=" + model.PaymentListId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

